﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KBManagement.DAL;
using KBManagement.Library;


namespace KBManagement
{
    public partial class AddEditExtraFees : Form
    {
        KBSQLClassDataContext context;
        public AddEditExtraFees()
        {
            context = new KBSQLClassDataContext();
            InitializeComponent();
            BindingComboBox();
        }

        private void BindingComboBox()
        {
            var category = from fc in context.CategoryExtraFees select fc;
            cbbCateId.DataSource = category;
            cbbCateId.DisplayMember = "CEF_Name";
            cbbCateId.ValueMember = "CEF_ID";

            var monthSalary = from fc in context.Pay_Salaries
                              group fc by fc.PayMonth into fcgr
                              orderby fcgr.Key descending
                             select new
                             {
                                 PayMonth = fcgr.Key
                             };

            cbbMonth.DataSource = monthSalary;
            cbbMonth.DisplayMember = "PayMonth";
            cbbMonth.ValueMember = "PayMonth";

            var yearSalary = from fc in context.Pay_Salaries
                             group fc by fc.PayYear into fcgr
                             orderby fcgr.Key descending
                             select new
                             {
                                 PayYear = fcgr.Key
                             };
            cbbYear.DataSource = yearSalary;
            cbbYear.DisplayMember = "PayYear";
            cbbYear.ValueMember = "PayYear";
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            bool isValid = validate();
            if (!isValid)
            {
                return;
            }

            InsertExtraFee();
        }


        private void InsertExtraFee()
        {

            var extraFree = new ExtraFee
            {
                EF_Name = txtNamePro.Text,
                EF_MONEY = Convert.ToDecimal(txtPricePro.Text),
                CEF_Status = true,
                CEF_ID = Convert.ToInt32(cbbCateId.SelectedValue.ToString()),
            };
            context.ExtraFees.InsertOnSubmit(extraFree);
            context.SubmitChanges();
            Utils.CreateInstance().NotificationInforObserver();
            Utils.CreateInstanceLog().TransactionLogCommon("Nhân viên " + AdminSession.admin.UserName + " thêm " + txtPricePro.Text + " phụ phí " + txtNamePro.Text + " vào lúc " + DateTime.Now);
            Close();
        }

        public bool validate()
        {
            if (txtNamePro.Text == "")
            {
                MessageBox.Show("Xin mời bạn điền tên cho dịch vụ");
                return false;
            }
            else if (txtPricePro.Text == "")
            {
                MessageBox.Show("Xin mời bạn điền giá cho dịch vụ");
                return false;
            }

            return true;
        }

        private void cbbMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
                
        }
    }
}
